package GUILayer;

import java.awt.*;
import ModelLayer.*;
import ControlLayer.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Dette er klassen CreateProduct.
 * 
 * @author Gruppe 4
 * @version 7/4 2014
 * 
 */
public class CreateProduct extends JFrame {
	private JFrame createProduct = new JFrame();
	private JTextField textCreateDescription;
	private JTextField textCreateName;
	private JTextField textCreateType;
	private JTextField textCreatePurchasePrice;
	private JTextField textCreateSalesPrice;
	private JTextField textCreateRentPrice;
	private JTextField textCreateCountryOfOrigin;
	private JTextField textCreateMinStock;
	private JTextField textCreateSupplierID;
	private JTextField textCreateStock;
	private ProductCtr productCtr;

	/**
	 * Denne metode laver et interface for klassen CreateProduct.
	 */
	public CreateProduct() {

		productCtr = ProductCtr.getInstance();
		createProduct.setVisible(true);
		createProduct.setSize(400, 200);
		createProduct.setResizable(false);
		createProduct.getContentPane().setLayout(new GridLayout(6, 8, 2, 2));

		JLabel lblCreateSupplierId = new JLabel("Supplier ID:");
		createProduct.getContentPane().add(lblCreateSupplierId);

		textCreateSupplierID = new JTextField();
		textCreateSupplierID.setColumns(10);
		createProduct.getContentPane().add(textCreateSupplierID);

		JLabel lblCreateName = new JLabel("Name:");
		createProduct.getContentPane().add(lblCreateName);

		textCreateName = new JTextField();
		textCreateName.setColumns(10);
		createProduct.getContentPane().add(textCreateName);

		JLabel lblCreateDescription = new JLabel("Description:");
		createProduct.getContentPane().add(lblCreateDescription);
		textCreateDescription = new JTextField();
		textCreateDescription.setColumns(10);
		createProduct.getContentPane().add(textCreateDescription);

		JLabel lblCreateSalesPrice = new JLabel("Sales Price:");
		createProduct.getContentPane().add(lblCreateSalesPrice);

		textCreateSalesPrice = new JTextField();
		textCreateSalesPrice.setColumns(10);
		createProduct.getContentPane().add(textCreateSalesPrice);

		JLabel lblCreateMinimumStock = new JLabel("Minimum Stock:");
		createProduct.getContentPane().add(lblCreateMinimumStock);

		textCreateMinStock = new JTextField();
		textCreateMinStock.setColumns(10);
		createProduct.getContentPane().add(textCreateMinStock);

		JLabel lblCreateStock = new JLabel("Stock:");
		createProduct.getContentPane().add(lblCreateStock);

		textCreateStock = new JTextField();
		textCreateStock.setColumns(10);
		createProduct.getContentPane().add(textCreateStock);

		JLabel lblCreatePurchasePrice = new JLabel("Purchase Price:");
		createProduct.getContentPane().add(lblCreatePurchasePrice);

		textCreatePurchasePrice = new JTextField();
		textCreatePurchasePrice.setColumns(10);
		createProduct.getContentPane().add(textCreatePurchasePrice);

		JLabel lblCreateCountryOfOrigin = new JLabel("Country of Origin:");
		createProduct.getContentPane().add(lblCreateCountryOfOrigin);

		textCreateCountryOfOrigin = new JTextField();
		textCreateCountryOfOrigin.setColumns(10);
		createProduct.getContentPane().add(textCreateCountryOfOrigin);

		JLabel lblCreateRentPrice = new JLabel("Rent Price:");
		createProduct.getContentPane().add(lblCreateRentPrice);

		textCreateRentPrice = new JTextField();
		textCreateRentPrice.setColumns(10);
		createProduct.getContentPane().add(textCreateRentPrice);

		JLabel lblCreateType = new JLabel("Type:");
		createProduct.getContentPane().add(lblCreateType);

		textCreateType = new JTextField();
		textCreateType.setColumns(10);

		JButton btnCreateNewProduct = new JButton("Create Product");
		btnCreateNewProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createNewProduct();
			}
		});

		JButton btnGoBack = new JButton("Go Back");
		btnGoBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createProduct.setVisible(false);
			}
		});

		createProduct.getContentPane().add(textCreateType);
		createProduct.getContentPane().add(btnGoBack);
		createProduct.getContentPane().add(btnCreateNewProduct);
		pack();
	}

	/**
	 * Denne metode laver et nyt objekt af klassen Product.
	 */
	private void createNewProduct() {
		String pName = textCreateName.getText();
		String pType = textCreateType.getText();
		int pPurchase = Integer.parseInt(textCreatePurchasePrice.getText());
		int pSales = Integer.parseInt(textCreateSalesPrice.getText());
		int pRent = Integer.parseInt(textCreateRentPrice.getText());
		String pCountry = textCreateCountryOfOrigin.getText();
		int pMin = Integer.parseInt(textCreateMinStock.getText());
		int pSupplier = Integer.parseInt(textCreateSupplierID.getText());
		int pStock = Integer.parseInt(textCreateStock.getText());
		productCtr.createProduct(pName, pType, pPurchase,
				pSales, pRent, pCountry, pMin, pStock, pSupplier);
		JOptionPane.showMessageDialog(null, "Succes!");
	}

}
